<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); ?>

<?php 
	$ci = & get_instance();
	$tid = intval($_GET['tid']);
	
	$TOURNAMENT = $ci->tournament_entity->init("id", $tid);
	
	if(mCrypt($tid) != $_GET['scrid']){
		redirect(site_url());exit();
	}	 

	$tournamentEventCategory = $ci->event_category_tournament_entity->getEnableCategories($tid);
	
	$manual_category_array = $ci->category_manual_tournament_entity->getOpenListCategories($TOURNAMENT->getId());
	
	$statusReturn = statusOfActionDataConfig();
	$object_result = $question = $isUserSetBetFee = '';
	if(isUserLogin()){
		$USER = $ci->user_entity->init("id", getUserId());
		//$object_result = $ci->user_tournament_entity->findUserInTournament( $USER->getId(), $TOURNAMENT->getId() );
		$isUserSetBetFee = $ci->user_tournament_entity->isUserSetBetFee($USER->getId(), $TOURNAMENT->getId());
		
		if(getEntryFeeDeducted($TOURNAMENT->getId()) > 0){
			$question = str_replace(array('%1'), array(getEntryFeeDeducted($TOURNAMENT->getId())), $statusReturn['question_join_in_new_tournament']);
		}else{
			$question = '';
		} //
	}
 
	function getPreOddsDefine(){
		return $preOddsDefineArray = array(
			'5'	=> 5,
			'10' => 10,
			'25' => 25,
			'50' => 50,
			'100' => 100,
			'200' => 200,
			'500' => 500,
			'1000' => 1000
		);
	}
	
	function buildBETpre($obj){
		$optionDD = form_dropdown('direct_setodds[]', getPreOddsDefine(), array() , "id=\"direct_stake_{$obj->id}\" class='direct_setoddsslb' rel=\"{$obj->id}\" onchange=\"dochange_DDvaluebet(this.value,'{$obj->id}')\" " );
		//<input type=\"text\" name=\"direct_setodds[]\" value=\"1\" class=\"direct_setoddsinput\" id=\"direct_stake_{$obj->id}\" rel=\"{$obj->id}\" />
		$cximage = loadCX();					
		$betFM = bet_format( 5*($obj->odds) );
		$html = "
			<div class=\"direct_bet hidden\" id=\"direct_bet_{$obj->id}\">
				<div class=\"item_direct_bet\" id=\"event_item_direct_{$obj->id}\" rel=\"{$obj->id}\">
					<div class=\"item_direct_bet_grid_close\" rel=\"{$obj->id}\" title=\"Close?\">&nbsp;</div>
					<div class=\"item_direct_bet_grid_odds\">
						<div class=\"box-item\">	
							{$optionDD}
							{$cximage} coins
						</div>
						<div class=\"box-item-to-arrow\">
						</div>
						<div class=\"box-item\">
							<input type=\"text\" name=\"direct_win[]\" value=\"{$betFM}\" class=\"direct_setoddsinput2\" disabled=\"disabled\" id=\"direct_win_{$obj->id}\" rel=\"{$obj->id}\" />
							Points
						</div>
						<input type=\"hidden\" name=\"direct_eventid[]\" value=\"{$obj->id}\" />
					</div>	
					<div class=\"item_direct_bet_place_tournament\" rel=\"{$obj->id}\" onclick=\"tournament_predict({$obj->id})\" title=\"Predict?\">&nbsp;</div>
				<!--	<div class=\"item_direct_bet_add_betslip\" rel=\"{$obj->id}\" title=\"Add to Prediction Slip?\">&nbsp;</div>
				-->
				</div>
			</div>
		";
		return $html;
	} 
	
	function buildManualBet($obj){
		$optionDD = form_dropdown('direct_setodds[]', getPreOddsDefine(), array() , "id=\"direct_stake_{$obj->key_id}\" class='direct_setoddsslb' rel=\"{$obj->key_id}\" onchange=\"dochange_DDvaluebet(this.value,'{$obj->key_id}')\"" );
		//<input type=\"text\" name=\"direct_setodds[]\" value=\"1\" class=\"direct_setoddsinput\" id=\"direct_stake_{$obj->key_id}\" rel=\"{$obj->key_id}\" />
		$cximage = loadCX();						
		$betFM = bet_format( 5*($obj->odds) );
		$html ="
			<div class=\"direct_bet hidden\" id=\"direct_bet_{$obj->key_id}\">
				<div class=\"item_direct_bet\" id=\"event_item_direct_{$obj->key_id}\" rel=\"{$obj->key_id}\">
					<div class=\"item_direct_bet_grid_close\" rel=\"{$obj->key_id}\" title=\"Close?\">&nbsp;</div>
					<div class=\"item_direct_bet_grid_odds\">
						<div class=\"box-item\">
							{$optionDD}
							{$cximage} coins
						</div> 
						<div class=\"box-item-to-arrow\"></div>
						<div class=\"box-item\">
							<input type=\"text\" name=\"direct_win[]\" value=\"{$betFM}\" class=\"direct_setoddsinput2\" disabled=\"disabled\" id=\"direct_win_{$obj->key_id}\" rel=\"{$obj->key_id}\" />
							Points
						</div>
						<input type=\"hidden\" name=\"direct_eventid[]\" value=\"{$obj->key_id}\" />
					</div>	
					<div class=\"item_direct_bet_place_manual_tournament\" rel=\"{$obj->key_id}\" onclick=\"tournament_predict_manual('{$obj->key_id}')\" title=\"Predict?\">&nbsp;</div>
				</div>
			</div>
		";
		return $html;
	}
	
	$margin_bottom = '10px';
	$statusArray = statusOfActionDataConfig();
	
	function showDenoteTitle($denote){
		$statusArray = statusOfActionDataConfig();
		return str_replace( array('%1', '%2'), array($denote,60+$denote),$statusArray['fe_D_defined'] );
	}
?>

<script type="text/javascript">
	<?php if( $isUserSetBetFee == 'no' AND $question AND isUserLogin()):?>
		;var PRE_QUESTION = '<?php echo $question?>';
	<?php else:?>
		;var PRE_QUESTION = '';
	<?php endif;?>	
</script>

<style>
	.show_id{
		position:absolute;
		top:2px;right:5px;
		font-weight:bold;
	}
</style>

<?php if(isUserLogin()): ?>
<?php 
	$TOURNAMENT = $this->tournament_entity->init('id',$tid);
	$USER = $this->user_entity->init('id',getUserId());
	
	$feeCoinMsg = "";
	$object_result = $this->user_tournament_entity->findUserInTournament( $USER->getId(), $tid );
	if(!$object_result){
		$changeMyCrashToINR = changeValueOfMoney($value= $USER->getCash(), $newCurrency='inr', $oldCurrency= $USER->getCurrencyCode());
		
		$mycoin = preCoinForUserBetEvent($tid);
		if($mycoin > 0){
			$feeCoinMsg = str_replace('%1',$mycoin,$statusReturn['coins_add_to_your_balance']);
		}
		$USER->setCoin($USER->getCoin()+$mycoin);
		$USER->update();
		
		$insertId = $this->user_tournament_entity->createNewUserTournamentEntity();
		$USER_TOURNAMENT = $this->user_tournament_entity->init('id',$insertId) ;
		$USER_TOURNAMENT->setTournamentId($tid);
		$USER_TOURNAMENT->setUserId($USER->getId());
		$USER_TOURNAMENT->update();
	}
?>

	<?php if($feeCoinMsg):?>	
		<!-- <div class="block">
			<div class="content">
				<h2 id="show_coins_add">
				&nbsp; 
				<?php //echo $feeCoinMsg;?>
				</h2>
			</div>
		</div> -->
		<script type="text/javascript">
			$(document).ready(function(){
				jQuery('#hiddenElement').html('<?php echo $feeCoinMsg;?>');
				jQuery('#hiddenElement').dialog(
					{
						 width: 350,
						 resizable: false,
						 height:200 ,
						 beforeClose: function(event, ui) { 
						 },
						  buttons: {
								"Ok": function() {
									$( this ).dialog( "close" );
								}
							}
						// title: '' 
					}
				);
			});
		</script>
	<?php endif; ?>

<?php endif; ?>


<div class="blockitem" style="margin-bottom:10px;">
	<h1 class="bold" style="font-size:14px;height:35px;text-align:center;position:relative;">
		<?php echo strtoupper($TOURNAMENT->getName());?> <br />
		<a href="<?php echo site_url("cricket/tournament/match/?tid=".$tid."&scrid=".mCrypt($tid));?>" style="color:#006699;" class="back_button" title="Back?">
			Back
		</a>
	</h1>
</div>

<div class="block">
	<div class="clrboth"></div>
	<?php foreach($manual_category_array as $item):?>
		<div class="title" style="position:relative;" >
			<span class="betticker" rel="div_toggle_<?php echo $item->key_id;?>">&nbsp;</span><h3 class="bold"><?php echo $item->category_name;?></h3>
		<!--	<span class="show_id">ID: <?php echo $item->key_id ? $item->key_id:$item->id;?></span>	-->
		</div>
		<div class="content" id="div_toggle_<?php echo $item->key_id;?>" style="margin-bottom:<?php echo $margin_bottom;?>;" >
			<input type="hidden" id="direct_event_type_<?php echo $item->key_id;?>" value="<?php echo $item->type; ?>" />
					
			<?php if($item->type == 'range'):?>
				<div class="grid3 clickbet event_<?php echo $item->key_id;?>" style="position:relative;line-height:25px;width:690px;" rel="<?php echo $item->key_id;?>">
					<input type="hidden" id="direct_event_odds_<?php echo $item->key_id;?>" value="<?php echo $item->odds; ?>" />
					<input type="hidden" id="direct_event_denote_<?php echo $item->key_id;?>" value="<?php echo $item->range_denote; ?>" />
				
					<span class="left bold" style="margin-left:5px;">
						From 
						<input type="text" class="boxes from_range_manual" rel="<?php echo $item->key_id;?>" value="" maxlength="25" name="range_<?php echo $item->key_id;?>[]" id="range_from_<?php echo $item->key_id;?>" />
						Runs
						&nbsp;&nbsp;&nbsp;&nbsp;
						To
						<input type="text" class="boxes" value="" disabled="disabled" maxlength="25" name="range_<?php echo $item->key_id;?>[]" id="range_to_<?php echo $item->key_id;?>" />
						Runs
					</span>
					<span class="right bold event_id_<?php echo $item->key_id;?>" style="margin-right:30px;color:#347235;">
						<span title="<?php echo showDenoteTitle($item->range_denote);?>"><?php echo $item->range_denote;?>D</span>
						&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						<?php echo bet_format($item->odds);?>
						<span style="color:#3f6d15;" title="<?php echo $statusArray['fe_P_defined'];?>">P</span>
					</span>
					
					<div class="predict_item"></div>
					
					<div class="clrboth"></div>
					<?php echo buildManualBet($item);?>
				</div>
			<?php endif;?>
			
			<?php if($item->type == 'box'):?>
				<div class="grid3 clickbet event_<?php echo $item->key_id;?>" style="position:relative;line-height:25px;width:690px;" rel="<?php echo $item->key_id;?>">
					<input type="hidden" id="direct_event_odds_<?php echo $item->key_id;?>" value="<?php echo $item->odds; ?>" />
					<input type="hidden" id="direct_event_number_boxes_<?php echo $item->key_id;?>" value="<?php echo $item->number_boxes; ?>" />
				
					<span class="left bold" style="margin-left:5px;width:570px;margin-top:2px;">
						<div class="center-align">
							<?php for($i=0,$j=1;$i< $item->number_boxes; $i++): ?>
								<input type="text" value="" maxlength="255" rel="<?php echo $item->key_id;?>" name="box_<?php echo $item->key_id;?>[]" id="boxes_<?php echo $item->key_id."_".$j++;?>" class="boxes input_box_manual" />
							<?php endfor;?>	
						</div>
					</span>
					<span class="right bold event_id_<?php echo $item->key_id;?>" style="margin-right:30px;color:#347235;">
						<?php echo bet_format($item->odds);?>
						<span style="color:#3f6d15;" title="<?php echo $statusArray['fe_P_defined'];?>">P</span>
					</span>
					
					<div class="clrboth"></div>
					<?php if($item->number_boxes >1):?>
						&nbsp;Note: you can enter up to <?php echo $item->number_boxes;?> Options, 
						Even if any one of your <?php echo $item->number_boxes;?> 
						Predictions is correct, you will earn Points
					<?php endif;?>
					<div class="clrboth"></div>
					
					<div class="predict_item"></div>
					
					<?php echo buildManualBet($item);?>
				</div>
			<?php endif;?>
		</div>
		<div class="clrboth"></div>
	<?php endforeach;?>
	
	<div class="clrboth"></div>
	<?php foreach($tournamentEventCategory as $item):?>
	
		<?php if($item->status == 'living' AND $item->is_open == 'yes') : ?>
			<div class="title" style="position:relative;" >
				<span class="betticker" rel="div_toggle_<?php echo $item->id;?>">&nbsp;</span><h3 class="bold"><?php echo $item->category;?></h3>
			<!--	<span class="show_id">ID: <?php echo $item->key_id ? $item->key_id:$item->id;?></span>	-->
			</div>
		<?php endif; ?>	
		
		<div class="clrboth"></div>
		<?php $event_array = $ci->event_tournament_entity->getArrayListOfEvent($item->id);?> 
			<?php if(!empty($event_array) AND $item->is_open == 'yes'):?>
				<div class="content" id="div_toggle_<?php echo $item->id;?>" style="margin-bottom:<?php echo $margin_bottom;?>;" >
					<?php $i=0; foreach($event_array as $event):?>
						<?php if($event->status == 'living' ) : ?>
							<?php 
								if($i%2 == 0){
									$borderR = "border-left:1px solid #cccccc;";
								}else{
									$borderR = "width:342px;border-left:1px solid #cccccc;";
								}
								$i++;
							?>
							<div class="grid3 clickbet event_<?php echo $event->id;?>" style="position:relative;line-height:25px;width:345px;border-bottom:1px solid #cccccc;<?php echo $borderR;?>" rel="<?php echo $event->id;?>">
								<input type="hidden" id="direct_event_odds_<?php echo $event->id;?>" value="<?php echo $event->odds; ?>" />
								
								<span class="left bold" style="margin-left:5px;" title="<?php echo $event->name;?>">
									<?php echo cutString($event->name,7);?>
								</span>
								<span class="right bold event_id_<?php echo $event->id;?>" style="margin-right:30px;color:#347235;">
									<?php echo bet_format($event->odds);?>
									<span style="color:#3f6d15;" title="<?php echo $statusArray['fe_P_defined'];?>">P</span>
								</span>
								
								<div class="predict_item"></div>
								
								<div class="clrboth"></div>
								
								<?php echo buildBETpre($event);?>
								
							</div>
						<?php endif; ?>	
					<?php endforeach; ?>
				</div>
			<?php endif; ?>	
	<?php endforeach;?>	
	
</div>	